// LN_BPA.cpp: implementation of the CLN_BPA class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../../Include/Swi/LN_BPA.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLN_BPA::CLN_BPA()
{
	m_chType = 'L';
	m_chSubType = 'N';
	m_chChangeCode = ' ';	

	memset(m_strName1,' ',8);
	m_strName1[8] = '\0';
	m_dkV1 = 0;

	memset(m_strName2,' ',8);
	m_strName2[8] = '\0';
	m_dkV2 = 0;

	memset(m_strName3,' ',8);
	m_strName3[8] = '\0';
	m_dkV3 = 0;

	memset(m_strName4,' ',8);
	m_strName4[8] = '\0';
	m_dkV4 = 0;

	memset(m_strName5,' ',8);
	m_strName5[8] = '\0';
	m_dkV5 = 0;

	m_strCardType[0] = 'L';
	m_strCardType[1] = 'N';
}

CLN_BPA::CLN_BPA(CLN_BPA &lnBPA):CCard_BPA(lnBPA)
{
	m_chType = lnBPA.m_chType;
	m_chSubType = lnBPA.m_chSubType;
	m_chChangeCode = lnBPA.m_chChangeCode;

	strcpy(m_strName1,lnBPA.m_strName1);
	m_dkV1 = lnBPA.m_dkV1;

	strcpy(m_strName2,lnBPA.m_strName2);
	m_dkV2 = lnBPA.m_dkV2;

	strcpy(m_strName3,lnBPA.m_strName3);
	m_dkV3 = lnBPA.m_dkV3;

	strcpy(m_strName4,lnBPA.m_strName4);
	m_dkV4 = lnBPA.m_dkV4;

	strcpy(m_strName5,lnBPA.m_strName5);
	m_dkV5 = lnBPA.m_dkV5;
}

CLN_BPA::~CLN_BPA()
{

}

bool CLN_BPA::Equals(CListItem *pItem)
{
	return true;
}

void CLN_BPA::OutputToScreen()
{
	char strAddition[10];
	memset(strAddition,'\0',10);
	switch(m_nCardState)
	{
	case Added:
		strcpy(strAddition,"Added");
		break;
	case Deleted:
		strcpy(strAddition,"Deleted");
		break;
	case Modified:
		strcpy(strAddition,"Modified");
		break;
	case Unchanged:
		strcpy(strAddition,"Unchanged");
		break;
	default:
		break;
	}
	printf("%s Card %s: %s & %f, %s & %f, %s & %f, %s & %f, %s & %f;\n",strAddition,m_strCardType, \
			m_strName1,m_dkV1,m_strName2,m_dkV2,m_strName3,m_dkV3,m_strName4,m_dkV4,m_strName5,m_dkV5);
}

bool CLN_BPA::FillEx(char *buffer)
{
	int i;

	if(EndOfSec(&buffer[0],1))
	{
		printf("Error in (CLN_BPA: FillEx): no type!\n");
		return false;
	}
	m_chType = buffer[0];

	if(EndOfSec(&buffer[1],1))
	{
		printf("Error in (CLN_BPA: FillEx): no subtype!\n");
		return false;
	}
	m_chSubType = buffer[1];

	if(EndOfSec(&buffer[2],1))
	{
		printf("Error in (CLN_BPA: FillEx): no change code!\n");
		return false;
	}
	m_chChangeCode = buffer[2];

	for(i=0;i<8;i++)
	{
		if(!buffer[3+i])
			break;
		else
			m_strName1[i] = buffer[3+i];
	}
	m_strName1[i] = '\0';
	if(i!=8)
	{
		printf("Error in (CLN_BPA: FillEx): no bus1 name!\n");
		return false;
	}
	
	m_dkV1 = StrToDouble(&buffer[11],4);
	if (m_dkV1 == 0)
	{
		printf("Error in (CLN_BPA: FillEx): bus1 voltage is zero!\n");
		return false;
	}

	if(EndOfSec(&buffer[11],7))
		return true;

	for(i=0;i<8;i++)
	{
		if(!buffer[18+i])
			break;
		else
			m_strName2[i] = buffer[18+i];
	}
	m_strName2[i] = '\0';
	/*if(i!=8)
	{
		printf("Error in (CLN_BPA: FillEx): no bus2 name!\n");
		return false;
	}*/
	if(EndOfSec(&buffer[18],8))
		return true;

	m_dkV2 = StrToDouble(&buffer[26],4);
	/*if (m_dkV2 == 0)
	{
		printf("Error in (CLN_BPA: FillEx): bus2 voltage is zero!\n");
		return false;
	}*/

	if(EndOfSec(&buffer[26],7))
		return true;

	for(i=0;i<8;i++)
	{
		if(!buffer[33+i])
			break;
		else
			m_strName3[i] = buffer[33+i];
	}
	m_strName3[i] = '\0';
	/*if(i!=8)
	{
		printf("Error in (CLN_BPA: FillEx): no bus3 name!\n");
		return false;
	}*/
	if(EndOfSec(&buffer[33],8))
		return true;
	
	m_dkV3 = StrToDouble(&buffer[41],4);
	/*if (m_dkV3 == 0)
	{
		printf("Error in (CLN_BPA: FillEx): bus3 voltage is zero!\n");
		return false;
	}*/

	if(EndOfSec(&buffer[41],7))
		return true;

	for(i=0;i<8;i++)
	{
		if(!buffer[48+i])
			break;
		else
			m_strName4[i] = buffer[48+i];
	}
	m_strName4[i] = '\0';
	/*if(i!=8)
	{
		printf("Error in (CLN_BPA: FillEx): no bus4 name!\n");
		return false;
	}*/
	if(EndOfSec(&buffer[48],8))
		return true;
	
	m_dkV4 = StrToDouble(&buffer[56],4);
	/*if (m_dkV4 == 0)
	{
		printf("Error in (CLN_BPA: FillEx): bus4 voltage is zero!\n");
		return false;
	}*/

	if(EndOfSec(&buffer[56],7))
		return true;

	for(i=0;i<8;i++)
	{
		if(!buffer[63+i])
			break;
		else
			m_strName5[i] = buffer[63+i];
	}
	m_strName5[i] = '\0';
	/*if(i!=8)
	{
		printf("Error in (CLN_BPA: FillEx): no bus5 name!\n");
		return false;
	}*/
	if(EndOfSec(&buffer[63],8))
		return true;

	m_dkV5 = StrToDouble(&buffer[71],4);
	/*if (m_dkV5 == 0)
	{
		printf("Error in (CLN_BPA: FillEx): bus5 voltage is zero!\n");
		return false;
	}*/

	return true;
}

bool CLN_BPA::UpdateEx(char *buffer)
{
	buffer[0] = m_chType;
	buffer[1] = m_chSubType;
	buffer[2] = m_chChangeCode;
	
	strcpy(&buffer[3],m_strName1);
	DoubleToStr(m_dkV1,&buffer[11],4);
	strcpy(&buffer[15],"   ");

	strcpy(&buffer[18],m_strName2);
	DoubleToStr(m_dkV2,&buffer[26],4);
	strcpy(&buffer[30],"   ");

	strcpy(&buffer[33],m_strName3);
	DoubleToStr(m_dkV3,&buffer[41],4);
	strcpy(&buffer[45],"   ");

	strcpy(&buffer[48],m_strName4);
	DoubleToStr(m_dkV4,&buffer[56],4);
	strcpy(&buffer[60],"   ");

	strcpy(&buffer[63],m_strName5);
	DoubleToStr(m_dkV5,&buffer[71],4);

	return true;
}
